package org.acb.sgp.dao.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.acb.sgp.beans.Utileria;
import org.acb.sgp.dao.UtileriaDao;
import org.acb.sgp.factory.ConnectionFactory;

public class UtileriaDaoImpl implements UtileriaDao {

    private Connection con;
    private CallableStatement cs;
    private ResultSet rs;

    private Connection getConnection() throws SQLException {
        return ConnectionFactory.getInstance().getConnection();
    }

    @Override
    public List<Utileria> buscarUtileriasPorCodEscena(Integer codEscena) {
        List<Utileria> utilerias = new ArrayList<Utileria>();
        try {
            String sql = "{Call BuscarUtileriaPorCodEscena(?)}";
            con = getConnection();
            cs = con.prepareCall(sql);
            cs.setInt(1, codEscena);
            rs = cs.executeQuery();
            while (rs.next()) {
                utilerias.add(llenar(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(UtileriaDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
        return utilerias;
    }

    private Utileria llenar(ResultSet rs) throws SQLException {
        Utileria utileria = new Utileria();
        utileria.setCodUtileria(rs.getInt("codUtileria"));
        utileria.setNomUtileria(rs.getString("nomUtileria"));
        utileria.getEstado().setCodEstado(rs.getInt("codEstado"));
        utileria.getEstado().setNomEstado(rs.getString("nomEstado"));
        utileria.getCategoria().setCodCategoria(rs.getInt("codCategoria"));
        utileria.getCategoria().setNomCategoria(rs.getString("nomCategoria"));
        utileria.getColor().setCodColor(rs.getInt("codColor"));
        utileria.getColor().setNomColor(rs.getString("nomColor"));
        utileria.setFlDisponible(rs.getInt("flDisponible"));
        utileria.setFlComprar(rs.getInt("flComprar"));
        utileria.setFlAlquiler(rs.getInt("flAlquiler"));
        utileria.setFecRegistro(rs.getTimestamp("fecRegistro"));
        return utileria;
    }

}
